package yuanshen;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;

import GetHtml.GetHtml;
import Model.YuanShenData;
import Model.avatar;
import Model.reliquarie;
import SkillModel.SkillData;
import SkillModel.skill;
import SkillModel.skillrst;

public class YuanShenStart {
	public static final String[] FILE_HEAD = {"人物", "稀有度", "等级", "好感", "武器名称", "武器稀有度", "武器等级", "圣遗物", "命座", "普攻", "E", "Q"};
	public static final String FILE_PATH = "原神.csv";

	public static void main(String[] args) {
		CSVPrinter csvPrinter = null;
		BufferedWriter filewriter = null;
		LinkedHashMap<String, Integer> reliquarie_counts = new LinkedHashMap<String, Integer>();
		int charactnum = 1;
		
		try {
			System.out.println("正在从米游社获取角色信息...");
			String htmlinfo = getCharacterInfo();
			YuanShenData data = JSON.parseObject(new ByteArrayInputStream(htmlinfo.getBytes("utf-8")), YuanShenData.class, Feature.AutoCloseSource);
			
			CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(FILE_HEAD);
			filewriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FILE_PATH, false),"gbk"));
			csvPrinter = new CSVPrinter(filewriter, csvFormat);
			
			for (avatar avatar : data.data.avatars) {
				String reliquariestr = "";
				List<String> record=new ArrayList<>();
				record.add(avatar.name);
				avatar.rarity = avatar.rarity > 5 ? 0 : avatar.rarity;
				record.add(String.valueOf(avatar.rarity));
				record.add(String.valueOf(avatar.level));
				record.add(String.valueOf(avatar.fetter));
				record.add(avatar.weapon.name + "*" + avatar.weapon.affix_level);
				record.add(String.valueOf(avatar.weapon.rarity));
				record.add(String.valueOf(avatar.weapon.level));
				// 计算圣遗物套装Start
				for (reliquarie reliquarie : avatar.reliquaries) {
					// 低于16级的圣遗物不统计
					if (reliquarie.level < 16) {
						continue;
					}
					if (reliquarie_counts.get(reliquarie.set.name) != null) {
						int value = reliquarie_counts.get(reliquarie.set.name).intValue();
						reliquarie_counts.put(reliquarie.set.name, value + 1);
					} else {
						reliquarie_counts.put(reliquarie.set.name, 1);
					}
				}
				for (String key : reliquarie_counts.keySet()) {
					if (reliquarie_counts.get(key) >=2) {
						if (!reliquariestr.equals("")) { 
							reliquariestr = reliquariestr.concat(" ");
						}
						reliquariestr = reliquariestr.concat(reliquarie_counts.get(key) + "*" + key);
					}
				}
				record.add(reliquariestr);
				reliquarie_counts.clear();
				// 计算圣遗物套装End
				record.add(String.valueOf(avatar.actived_constellation_num));
				// 获取角色技能等级
				System.out.println("获取角色" + avatar.name + "技能等级(" + charactnum + "/" + data.data.avatars.size() + ")");
				skillrst tmprst = getSkill(avatar.id);
				record.add(String.valueOf(tmprst.level[0]));
				record.add(String.valueOf(tmprst.level[1]));
				record.add(String.valueOf(tmprst.level[2]));
				csvPrinter.printRecord(record);
				charactnum++;
			}
			if(null != filewriter) filewriter.flush();
			if(null != filewriter) filewriter.close();
			if(null != csvPrinter) csvPrinter.close();
			
			System.out.println("成功了!!!");
		} catch (Exception e) {
			System.out.println("Error:" + e.getMessage());
		}
		return;
	}
	
	public static String getCharacterInfo() {
		GetHtml.setRequestProperty("Accept", "application/json, text/plain, */*");
		GetHtml.setRequestProperty("x-rpc-client_type", "5");
		GetHtml.setRequestProperty("Accept-Language", "zh-CN,zh-Hans;q=0.9");
		GetHtml.setRequestProperty("Content-Type", "application/json;charset=utf-8");
		GetHtml.setRequestProperty("Origin", "https://webstatic.mihoyo.com");
		GetHtml.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/2.26.1");
		GetHtml.setRequestProperty("Referer", "https://webstatic.mihoyo.com/");
		GetHtml.setRequestProperty("x-rpc-app_version", "2.26.1");
		GetHtml.setRequestProperty("Content-Length", "42");
		GetHtml.setRequestProperty("Connection", "keep-alive");
		GetHtml.setRequestProperty("DS", Config.getDS());
		GetHtml.setRequestProperty("Cookie", Config.token);
		GetHtml.setRequestProperty("X-Requested-With", "com.mihoyo.hyperion");
		
		String inputstr = Config.combineStr;
		String url = "https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/character";
		String htmlinfo = GetHtml.postHtmlInfo(url, inputstr, "utf-8", "utf-8");
		
		return htmlinfo;
	}

	public static skillrst getSkill(int charactid) throws Exception
	{
		skillrst skillrst = new skillrst();
		int skillnum = 0;
		
		String url = "https://api-takumi.mihoyo.com/event/e20200928calculate/v1/sync/avatar/detail?avatar_id=" + charactid + "&uid=" + Config.uid + "&region=" + Config.server;
		GetHtml.setRequestProperty("Cookie",  Config.token);
		GetHtml.setRequestProperty("X-Requested-With", "com.mihoyo.hyperion");
		GetHtml.setRequestProperty("Referer", "https://webstatic.mihoyo.com/ys/event/e20200923adopt_calculator/index.html?bbs_presentation_style=fullscreen&bbs_auth_required=true&utm_source=bbs&utm_medium=mys&utm_campaign=icon");
		String htmlinfo = GetHtml.getHtmlInfo(url, "utf-8");
		
		SkillData data = JSON.parseObject(new ByteArrayInputStream(htmlinfo.getBytes("utf-8")), SkillData.class, Feature.AutoCloseSource);	
		for (skill tmpskill : data.data.skill_list) {
			if (tmpskill.max_level > 1) {
				skillrst.level[skillnum] = tmpskill.level_current;
				skillnum++;
			}
			if (skillnum == 3) {
				break;
			}
		}
		
		return skillrst;
	}
}
